<!--
 * @Descripttion: 系统计划任务配置
 * @version: 1.0
 * @Author: sakuya
 * @Date: 2021年7月7日09:28:32
 * @LastEditors:
 * @LastEditTime:
-->

<template>
	<el-container>
		<el-main style="padding:0 20px;">
			<el-row :gutter="20">
				<el-col :span="24">
					<el-form :inline="true" class="content-input-length" label-width="68px">
						<el-form-item label="调度状态" style="margin-left:10px">
							<el-select placeholder="请选择" v-model="search.logStatus" clearable>
								<el-option label="全部" value="-1"/>
								<el-option label="成功" value="1"/>
								<el-option label="失败" value="2"/>
							</el-select>
						</el-form-item>
						<el-form-item label="调度时间" style="width: 470px">
							<el-date-picker
								v-model="search.triggerTime"
								:clearable="false"
								type="datetimerange"
								range-separator="至"
								start-placeholder="开始日期"
								end-placeholder="结束日期">
							</el-date-picker>
							<span style="padding-left: 45px;position: absolute; top: -1.5px;left: 400px;width: 150px">
								<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
								<el-button icon="el-icon-refresh" @click="searchReset"></el-button>
							</span>
						</el-form-item>

					</el-form>
				</el-col>
			</el-row>

			<scTable ref="table" :apiObj="apiObj" :params="search" stripe hide-export="true" hide-import="true" height="90%" pageSize="10">
				<el-table-column label="调度时间" prop="triggerTime"></el-table-column>
				<el-table-column label="调度结果" prop="triggerCode">
					<template #default="scope">
						<span v-if="scope.row.triggerCode==''"></span>
						<span v-else-if="scope.row.triggerCode==200" style="color: #67C23A;">成功</span>
						<span v-else style="color: #F56C6C;">失败</span>
					</template>
				</el-table-column>
				<el-table-column label="调度备注" prop="triggerMsg">
					<template #default="scope">
						<span v-if="!scope.row.triggerMsg">无</span>
						<el-button v-else size="small" @click="show('调度备注', scope.row.triggerMsg)" type="text">
							查看
						</el-button>
					</template>
				</el-table-column>

				<el-table-column label="执行时间" prop="handleTime"></el-table-column>
				<el-table-column label="执行结果" prop="handleCode">
					<template #default="scope">
						<span v-if="scope.row.handleCode==''"></span>
						<span v-else-if="scope.row.handleCode==200" style="color: #67C23A;">成功</span>
						<span v-else style="color: #F56C6C;">失败</span>
					</template>
				</el-table-column>
				<el-table-column label="执行备注" prop="handleMsg">
					<template #default="scope">
						<span v-if="!scope.row.handleMsg">无</span>
						<el-button v-else size="small" @click="show('执行备注', scope.row.handleMsg)" type="text">查看
						</el-button>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>

	<el-drawer :title="titleMsg" v-model="logsVisible" :size="700" direction="rtl" destroy-on-close>
		<el-main style="padding:0 20px 20px 20px;">
			<pre
				style="font-size: 12px;color: #999;padding:20px;background: #333;font-family: consolas;line-height: 1.5;overflow: auto;">
				<div style="position: relative; top:-16px"><span v-html="demoLog"></span></div>
			</pre>
		</el-main>
	</el-drawer>
</template>

<script>
export default {
	data() {
		return {
			logsVisible: false,
			demoLog: "",
			data: [],
			apiObj: this.$API.system.task.log,
			search: {
				jobId: "-1",
				logStatus: "-1",
				triggerTime: "",
				triggerTimeStart: "",
				triggerTimeEnd: ""
			},
			titleMsg: ""
		}
	},
	mounted() {

	},
	methods: {
		show(title, msg) {
			this.logsVisible = true;
			this.titleMsg = title;
			this.demoLog = msg
		},

		/**
		 * 搜索
		 */
		async upsearch() {
			if (!this.search.triggerTime) {
				this.search.triggerTime = this.getTimeRange()
			}
			let requestParams = Object.assign({}, this.search);
			if ((typeof requestParams.triggerTime) != "string") {
				let startTime = this.getYearMethodDay(requestParams.triggerTime[0]);
				let endTime = this.getYearMethodDay(requestParams.triggerTime[1]);
				requestParams.triggerTime = "";
				requestParams.triggerTimeStart = startTime;
				requestParams.triggerTimeEnd = endTime;
				if (this.getDateSize(new Date(startTime), new Date(endTime))) {
					this.$message.warning('"操作时间"跨度不可超过一个月')
					return ;
				}
			}
			this.$refs.table.upData(requestParams)
		},

		/**
		 * 搜索条件重置
		 */
		async searchReset() {
			let timeRanges = this.getTimeRange();
			this.search = {
				jobId: this.search.jobId,
				logStatus: "-1",
				triggerTime: timeRanges,
				triggerTimeStart: timeRanges[0],
				triggerTimeEnd: timeRanges[1]
			}
			this.upsearch();
		},

		/**
		 * 表单注入数据
		 * @param data
		 */
		setData(data) {
			this.search.jobId = data.id
			this.searchReset();
		},

		/**
		 * 获取时间跨度
		 * @returns {Date[]} [开始时间,结束时间]数组结构
		 */
		getTimeRange() {
			var endTime = new Date();
			var startTime = new Date(new Date().setDate(endTime.getDate() - 14))
			return [startTime, endTime]
		},

		getYearMethodDay(date) {
			//方法里面可以分为行参、实参
			if (date == undefined) {
				date = new Date();
			}
			let nian = date.getFullYear();
			let yue = parseInt(date.getMonth()) + 1;
			let ri = date.getDate();
			var hour = date.getHours(); // 时
			var minutes = date.getMinutes(); // 分
			var seconds = date.getSeconds() //秒

			if (yue >= 1 && yue <= 9) {
				yue = "0" + yue;
			}
			if (ri >= 0 && ri <= 9) {
				ri = "0" + ri;
			}
			if (hour >= 0 && hour <= 9) {
				hour = "0" + hour;
			}
			if (minutes >= 0 && minutes <= 9) {
				minutes = "0" + minutes;
			}
			if (seconds >= 0 && seconds <= 9) {
				seconds = "0" + seconds;
			}
			let jia = nian + "-" + yue + "-" + ri + " " + hour + ":" + minutes + ":" + seconds
			return jia;
		},

		getDateSize(startDate, endDate) {
			var sDate = new Date(startDate);
			var eDate = new Date(endDate);

			if (eDate.getFullYear() - sDate.getFullYear() > 1) {//先比较年
				return true;
			} else if (eDate.getMonth() - sDate.getMonth() > 1) {//再比较月
				return true;
			} else if (eDate.getMonth() - sDate.getMonth() == 1) {
				if (eDate.getDate() - sDate.getDate() >= 1) {
					return true;
				}
			} else if (eDate.getFullYear() - sDate.getFullYear() == 1) {
				if (eDate.getMonth() + 12 - sDate.getMonth() > 1) {
					return true;
				} else if (eDate.getDate() - sDate.getDate() >= 1) {
					return true;
				}
			}
			return false;
		}
	}
}
</script>

<style>
</style>
